package com.java.dormitorymanager.mvc.view;

import com.java.dormitorymanager.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 学生管理界面
 * @author: ShuangChenYue
 * @create: 2021-05-16 19:15
 **/
@WebServlet(value = "/ViewManageStudent")
public class ViewManageStudentServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        System.out.println("您已进入学生管理界面");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("<title>学生管理界面</title>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-student/manageStudent.css\">");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-other/doIcon.css\">");
        out.println("</head>");
        out.println("<body>");
        out.println("<div id=\"divTitle\">");
        out.println("<h1 id=\"h01\"> Manage Students</h1>");
        out.println("</div>");
        out.println("<div id=\"div02\">");
        out.println("<form action=\"/DormitoryManager/mvc/searchStudentByName.do\" method=\"post\">");
        out.println("<input id=\"inputSearch01\" type=\"text\" name=\"name\" placeholder=\"输入姓名查找学生\"><input class=\"iconfontU\" id=\"inputSearch02\" type=\"submit\" name=\"search\" value=\"&#xe632;\" title=\"点击搜索\">");
        out.println("</form>");
        out.println("</div>");
        out.println("<div id=\"div01\">");
        out.println("<h3 class=\"ListName\">Male</h3>");
        out.println("<a  class=\"iconfontU addIcon\" title=\"添加学生\" href=\"/DormitoryManager/ViewAddStudent\">&#xe621;</a>");
        out.println("<table class=\"table01\">");
        out.println("<tr class=\"tr01\">");
        out.println("<td>ID</td>");
        out.println("<td>姓名</td>");
        out.println("<td>学号</td>");
        out.println("<td>年龄</td>");
        out.println("<td>操作</td>");
        out.println("</tr>");
        List<Student> allMaleStudentsList = (List<Student>) req.getAttribute("allMaleStudentsList");
        Iterator<Student> iterator = allMaleStudentsList.iterator();
        while (iterator.hasNext())
        {
            Student maleStudent = iterator.next();
            out.println("<tr class=\"tr02\">");
            out.println("<td style=\"text-align: center\">"+maleStudent.getId()+"</td>");
            out.println("<td>"+maleStudent.getName()+"</td>");
            out.println("<td>"+maleStudent.getStunum()+"</td>");
            out.println("<td>"+maleStudent.getAge()+"</td>");
            out.println("<td><a href=\"/DormitoryManager/mvc/toUpdateStudent.do?id="+maleStudent.getId()+"\"><i class=\"iconfontU alterIcon\" title=\"修改\">&#xe700;</i></a> <a href=\"/DormitoryManager/mvc/deleteStudent.do?id="+maleStudent.getId()+"\"><i class=\"iconfontU deleteIcon\" title=\"删除\">&#xe614;</i></a> <a href=\"/DormitoryManager/mvc/showStudentDetail.do?id="+maleStudent.getId()+"\"><i class=\"iconfontU infoIcon\" title=\"详情\">&#xe611;</i></a></td>");
            out.println("</tr>");
        }

        out.println("</table>");


        out.println("<h3 class=\"ListName\">Female</h3>");
        out.println("<table class=\"table01\">");
        out.println("<tr class=\"tr01\">");
        out.println("<td>ID</td>");
        out.println("<td>姓名</td>");
        out.println("<td>学号</td>");
        out.println("<td>年龄</td>");
        out.println("<td>操作</td>");
        out.println("</tr>");

        List<Student> allFemaleStudentsList = (List<Student>) req.getAttribute("allFemaleStudentsList");
        Iterator<Student> iterator1 = allFemaleStudentsList.iterator();
        while (iterator1.hasNext())
        {
            Student femaleStudent = iterator1.next();
            out.println("<tr class=\"tr02\">");
            out.println("<td style=\"text-align: center\">"+femaleStudent.getId()+"</td>");
            out.println("<td>"+femaleStudent.getName()+"</td>");
            out.println("<td>"+femaleStudent.getStunum()+"</td>");
            out.println("<td>"+femaleStudent.getAge()+"</td>");
            out.println("<td><a href=\"/DormitoryManager/mvc/toUpdateStudent.do?id="+femaleStudent.getId()+"\"><i class=\"iconfontU alterIcon\" title=\"修改\">&#xe700;</i></a> <a href=\"/DormitoryManager/mvc/deleteStudent.do?id="+femaleStudent.getId()+"\"><i class=\"iconfontU deleteIcon\" title=\"删除\">&#xe614;</i></a> <a href=\"/DormitoryManager/mvc/showStudentDetail.do?id="+femaleStudent.getId()+"\"><i class=\"iconfontU infoIcon\" title=\"详情\">&#xe611;</i></a></td>");
            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
